var form;
var tableIns;
var ctxPath = '/'
$(function () {
    layui.config({
        base: ctxPath + 'lib/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index'//主入口模块
    }).use(['index', 'table', 'form'], function () {
        var table = layui.table;
        form = layui.form;
        // 渲染表格
        tableIns = table.render({
            elem: '#table',
            toolbar: '#table-title',
            url: 'interLogPager',
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'jkid', title: '接口ID', align: 'center'},
                {field: 'requester', title: '调用方', align: 'center'},
                {field: 'terminal_id', title: '调用IP', align: 'center'},
                {
                    field: 'interface_time', title: '调用时间', align: 'center', templet: function (d) {
                        var value = d.interface_time;
                        return value.substring(0, 4) + '-'
                            + value.substring(4, 6)
                            + '-'
                            + value.substring(6, 8)
                            + '  '
                            + value.substring(8, 10)
                            + ':'
                            + value.substring(10, 12)
                            + ':'
                            + value.substring(12, 14);
                    }
                },
                {field: 'interface_condition', title: '请求内容', align: 'center'},
                {field: 'interface_result', title: '接口返回', align: 'center'},
                {field: 'right', title: '操作', toolbar: '#table-toolbar', align: 'center'},
            ]],
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'limit' //每页数据量的参数名，默认：limit
            }
        });
        //监听行工具事件
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'view') {
                openView(data)
            }
        });
        //监听提交
        form.on('submit(searchSubmit)', function () {
            tableIns.reload({
                where: {
                    requester: $("#requesterSearch").val(),
                    terminal_id: $("#terminalIdSearch").val()
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
    });
})

/**
 * 打开编辑窗口
 * @param data layui中保存的table中的对应的此条信息
 */
function openView(data) {
    layer.open({
        type: 1,
        title: "接口日志信息",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['700px', '500px'],
        content: $('#interLogEdit'),
        success: function () {
            form.val('interLogForm', data);
            renderForm()
        }, end: function () {
            cleanForm("interLogForm")
        }
    });
}

function initOrganSelect() {
    $.ajax({
        type: "GET",
        url: ctxPath + "system/organization/organList",
        success: function (res) {
            var data = res.obj;
            for (var i = 0; i < data.length; i++) {
                $("#cyqxhSearch").append("<option value='" + data[i].cyqxh + "'>" + data[i].name + "</option>")
            }
            renderForm();
        }
    });
}